package top.carrotguo.weibo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;
import top.carrotguo.weibo.bean.PersonListBean;

public class PersonToolsAdapter extends RecyclerView.Adapter<PersonToolsAdapter.ToolHolder> {

    private Context mContext;
    private ArrayList<PersonListBean> mDatas;
    private ToolsClickListenr mListener;

    public PersonToolsAdapter(Context context, ArrayList<PersonListBean> datas){
        this.mContext = context;
        this.mDatas = datas;
    }

    @NonNull
    @Override
    public PersonToolsAdapter.ToolHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view =LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_tools,viewGroup,false);
        return new ToolHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PersonToolsAdapter.ToolHolder viewHolder, int i) {
        boolean hasDivider = mDatas.get(i).isHasDivider();
        if (hasDivider) {
            viewHolder.divider.setVisibility(View.VISIBLE);
        } else {
            viewHolder.divider.setVisibility(View.GONE);
        }
        viewHolder.ivIcon.setBackgroundResource(mDatas.get(i).getIcon());
        viewHolder.tvName.setText(mDatas.get(i).getTitle());
        if (!TextUtils.isEmpty(mDatas.get(i).getDes())) {
            viewHolder.tvDes.setVisibility(View.VISIBLE);
            viewHolder.tvDes.setText(mDatas.get(i).getDes());
        } else {
            viewHolder.tvDes.setVisibility(View.INVISIBLE);
        }

        //点击事件
        viewHolder.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(i);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setToolClickListener(ToolsClickListenr listener){
        this.mListener = listener;
    }

    public interface ToolsClickListenr{
        void onClick(int position);
    }

    class ToolHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.divider)
        View divider;
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_des)
        TextView tvDes;
        @BindView(R.id.ll_item)
        LinearLayout llItem;

        public ToolHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
